#region Copyright(c) 2006 ZO, All right reserved.
// -----------------------------------------------------------------------------
// Copyright(c) 2006 ZO, All right reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//   1.  Redistribution of source code must retain the above copyright notice,
//       this list of conditions and the following disclaimer.
//   2.  Redistribution in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//   3.  The name of the author may not be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// -----------------------------------------------------------------------------
#endregion


#region Using directives
using System;
using System.Collections.Generic;
using System.Text;
using ArgumentNullException = ZO.SmartCore.Core.ArgumentNullException;
using ArgumentException = ZO.SmartCore.Core.ArgumentException;
using ArgumentOutOfRangeException = ZO.SmartCore.Core.ArgumentOutOfRangeException;
using ZO.SmartCore.Logger.Properties;
using System.Globalization;
using ZO.SmartCore.Helpers;
#endregion

namespace ZO.SmartCore.Logger
{
    /// <summary>
    /// Represents a text based formatter for Log messages.
    /// </summary>
    public class TextLogFormat : ILogFormat
    {

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="TextLogFormat"/> class.
        /// </summary>
        public TextLogFormat() : this(Resources.DefaultTextLogFormat)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="TextLogFormat"/> class.
        /// </summary>
        /// <param name="template">Template to be used when formatting.</param>
        public TextLogFormat(string template)
        {
            if (!string.IsNullOrEmpty(template))
            {
                this.Template = template;
            }
            else
            {
                this.Template = Resources.DefaultTextLogFormat;
            }
        }



        #endregion

        #region Destructor

        #endregion

        #region Fields
        private string _Template;
        #endregion

        #region Events

        #endregion

        #region Operators

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the formatting template.
        /// </summary>
        public string Template
        {
            get
            {
                return this._Template;
            }
            set
            {
                this._Template = value;
            }
        }


        #endregion

        #region Methods

        /// <summary>
        /// Formats the <see cref="T:Microsoft.Practices.EnterpriseLibrary.Logging.LogEntry" /> object by replacing tokens with values writing the format result
        /// to a <see cref="T:System.Text.StringBuilder" />.
        /// </summary>
        /// <param name="templateBuilder">The <see cref="T:System.Text.StringBuilder" /> that holds the formatting result.</param>
        /// <param name="log">Log entry to format.</param>
        /// <returns>Formatted string with tokens replaced with property values.</returns>
        protected virtual string Format(StringBuilder templateBuilder, LogEntry log)
        {
            templateBuilder.Replace("{timestamp}", log.TimeStampString);
            templateBuilder.Replace("{title}", log.Title);
            templateBuilder.Replace("{message}", log.Message);
            templateBuilder.Replace("{severity}", log.Severity.ToString());
            templateBuilder.Replace("{machine}", log.MachineName);
            templateBuilder.Replace("{appDomain}", log.AppDomainName);
            templateBuilder.Replace("{processId}", NumberHelper.ToString(log.ProcessID));
            templateBuilder.Replace("{processName}", log.ProcessName);
            templateBuilder.Replace("{threadName}", log.ManagedThreadName);
            templateBuilder.Replace("{win32ThreadId}", NumberHelper.ToString(log.Win32ThreadID));
            templateBuilder.Replace("{newline}", Environment.NewLine);
            templateBuilder.Replace("{tab}", "\t");
            return templateBuilder.ToString();
        }


        /// <summary>
        /// Creates a new builder to hold the formatting results based on the receiver's template.
        /// </summary>
        /// <returns>The new <see cref="T:System.Text.StringBuilder" />.</returns>
        protected StringBuilder CreateTemplateBuilder()
        {
            return new StringBuilder(((this.Template == null) || (this.Template.Length > 0)) ? 
                this.Template : Resources.DefaultTextLogFormat);
        }

 

        #endregion

        #region ILogFormat Members


        /// <summary>
        /// Formats a log entry and return a string to be outputted.
        /// </summary>
        /// <param name="log">Log entry to format.</param>
        /// <returns>A string representing the log entry.</returns>
        public string Format(LogEntry log)
        {
            return this.Format(this.CreateTemplateBuilder(), log);
        }

   

        #endregion
    }
}
